﻿using System;
using System.IO;
using System.Web;
using System.Web.Hosting;
using EPiServer.Security;
using EPiServer.Web.Hosting;
using Hosting.FlickrObjects;

namespace Hosting
{
    public class FlickrFile : UnifiedFile
    {
        private string _permanentLinkVirtualPath;
        private byte[] _content;
        private FlickrSummary _summary;
        private Photo _photo;
        private FileInfo _fileInfo;

        public FlickrFile(UnifiedDirectory directory, VirtualPathUnifiedProvider provider, string virtualPath, bool bypassAccessCheck, Photo photo) : base(directory, provider, virtualPath, bypassAccessCheck)
        {
            _permanentLinkVirtualPath = String.Empty;
            _photo = photo;
            _summary = new FlickrSummary();

            if(photo != null)
            {
                var localPath = ((FlickrVirtualPathProvider)Provider).PhysicalPath + "\\" + photo.Title;
                EnsureFileIsDownloaded(localPath);

                _fileInfo = new FileInfo(localPath);
            }
        }

        public override void CopyTo(string virtualDir)
        {
        }

        public override void CopyTo(string virtualDir, Guid fileId)
        {
        }

        public override void Delete()
        {
        }

        public override void MoveTo(string virtualDir)
        {
        }

        protected override void OnDeleted(UnifiedVirtualPathEventArgs e)
        {
        }

        protected override void OnDeleting(UnifiedVirtualPathEventArgs e)
        {
        }

        public override string PermanentLinkVirtualPath
        {
            get
            {
                return _permanentLinkVirtualPath;
            }
        }

        public override UnifiedDirectory Parent
        {
            get
            {
                return (HostingEnvironment.VirtualPathProvider.GetDirectory(VirtualPathUtility.GetDirectory(VirtualPath)) as FlickrDirectory);
            }
        }

        internal byte[] Content
        {
            get
            {
                return (_content ?? new byte[0]);
            }
            set
            {
                _content = value;

                throw new NotSupportedException("Upload is not supported.");

                //FlickrDataFactory.Instance.UploadPhoto(Name, _content);
            }
        }

        public override long Length
        {
            get
            {
                return _fileInfo.Length;
            }
        }

        public override DateTime Created
        {
            get
            {
                return _fileInfo.CreationTime;
            }
        }

        public override DateTime Changed
        {
            get
            {
                return _fileInfo.LastWriteTime;
            }
        }

        public override IUnifiedSummary Summary
        {
            get
            {
                return _summary;
            }
        }
        public override AccessLevel QueryAccess()
        {
            return AccessLevel.Read;
        }

        public override Stream Open()
        {
            return Open(FileMode.Open, FileAccess.Read, FileShare.Read);
        }

        public override Stream Open(FileMode mode)
        {
            return Open(mode, FileAccess.Read, FileShare.Read);
        }

        public override Stream Open(FileMode mode, FileAccess access)
        {
            return Open(mode, access, FileShare.Read);
        }

        public override Stream Open(FileMode mode, FileAccess access, FileShare share)
        {
            if(access == FileAccess.Read)
            {
                var localPath = ((FlickrVirtualPathProvider)Provider).PhysicalPath + "\\" + Name;
                EnsureFileIsDownloaded(localPath);

                return new FileStream(localPath, FileMode.Open);
            }

            return new FlickrStream(this, true);
        }

        private void EnsureFileIsDownloaded(string localPath)
        {
            if (!File.Exists(localPath))
            {
                if (!Directory.Exists(((FlickrVirtualPathProvider)Provider).PhysicalPath))
                {
                    Directory.CreateDirectory(((FlickrVirtualPathProvider)Provider).PhysicalPath);
                }

                FlickrDataFactory.Instance.DownloadPhoto(_photo, localPath);
            }

        }

    }
}
